package com.lrabbit.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lrabbit.msg.CommResponse;
import com.lrabbit.msg.CommResponseWithExtension;
import com.lrabbit.page.PagingResult;
import com.lrabbit.utils.BeanUtil;

import java.util.List;

/**
 * 基础控制器类，用于处理请求和响应
 */
public class BaseController {

    private static final String SUCCESS_CODE = "00000";
    private static final String SUCCESS_CODE_DESC = "操作成功";
    private static final String FAIL_CODE = "40000";
    private static final String FAIL_CODE_DESC = "操作失败";

    /**
     * 错误结果：返回无data
     *
     * @return 返回一个CommResponse实例
     */
    public CommResponse backError() {
        return new CommResponse(FAIL_CODE, FAIL_CODE_DESC);
    }

    /**
     * 错误结果：返回有数据
     *
     * @param errorMessage 错误信息
     * @return 返回一个CommResponse实例
     */
    public CommResponse backError(String errorMessage) {
        return new CommResponse(FAIL_CODE, errorMessage);
    }

    /**
     * 错误信息返回
     *
     * @param code     错误码
     * @param errorMsg 错误信息
     * @return 返回一个CommResponse实例
     */
    public CommResponse backError(String code, String errorMsg) {
        return new CommResponse(code, errorMsg);
    }

    /**
     * 正确结果：返回无data
     *
     * @return 返回一个CommResponse实例
     */
    public CommResponse back() {
        return new CommResponse(SUCCESS_CODE, SUCCESS_CODE_DESC);
    }

    /**
     * 正确结果：返回含data
     *
     * @param object 源数据
     * @return 返回一个CommResponse实例
     */
    public CommResponse back(Object object) {
        CommResponse commResponse = new CommResponse<>(SUCCESS_CODE, SUCCESS_CODE_DESC);

        if (object instanceof Page) {
            commResponse.setPagingResult(new PagingResult(
                    (int) ((Page<?>) object).getCurrent(),
                    (int) ((Page<?>) object).getSize(),
                    (int) ((Page<?>) object).getTotal()));
            commResponse.setData(((Page) object).getRecords());
        } else {
            commResponse.setData(object);
        }
        return commResponse;
    }

    /**
     * 返回含data data需要进行类型转换
     *
     * @param object  源数据
     * @param voClass 需要转换类型
     * @return 返回一个CommResponse实例
     */
    public CommResponse back(Object object, Class voClass) {
        CommResponse commResponse = new CommResponse(SUCCESS_CODE, SUCCESS_CODE_DESC);

        if (object instanceof Page) {
            commResponse.setPagingResult(new PagingResult(
                    // 当前页
                    (int) ((Page<?>) object).getCurrent(),
                    // 每页显示多少条
                    (int) ((Page<?>) object).getSize(),
                    // 总记录数
                    (int) ((Page<?>) object).getTotal()));
            commResponse.setData(BeanUtil.copyToList(((Page<?>) object).getRecords(), voClass));
        } else if (object instanceof List) {
            commResponse.setData(BeanUtil.copyToList((List<?>) object, voClass));
        } else {
            commResponse.setData(BeanUtil.copyObject(object, voClass));
        }
        return commResponse;
    }

    /**
     * 返回含data data需要进行类型转换
     *
     * @param object      源数据
     * @param voClass     需要转换类型
     * @param extension   扩展：使用场景例如：当需要返回查询的统计信息时
     * @param extendClass 转换后的类型
     * @return 返回一个CommResponseWithExtension实例
     */
    @Deprecated
    public CommResponseWithExtension backWithExtension(Object object, Class voClass,
                                                       Object extension, Class extendClass) {
        CommResponseWithExtension commResponse = new CommResponseWithExtension(SUCCESS_CODE, SUCCESS_CODE_DESC);
        commResponse.setData(BeanUtil.copyObject(extension, extendClass));
        if (object instanceof Page) {
            commResponse.setPagingResult(new PagingResult(
                    (int) ((Page<?>) object).getCurrent(),
                    (int) ((Page<?>) object).getSize(),
                    (int) ((Page<?>) object).getTotal()));
            commResponse.setData(BeanUtil.copyToList(((Page<?>) object).getRecords(), voClass));
        } else if (object instanceof List) {
            commResponse.setData(BeanUtil.copyToList((List<?>) object, voClass));
        } else {
            commResponse.setData(BeanUtil.copyObject(object, voClass));
        }
        return commResponse;
    }

    /**
     * 跨微服务查询数据分页
     *
     * @param page    分页对象
     * @param object  源数据
     * @param voClass 需要转换类型
     * @return 返回一个CommResponse实例
     */
    @Deprecated
    public CommResponse back(Page page, Object object, Class voClass) {
        CommResponse commResponse = new CommResponse(SUCCESS_CODE, SUCCESS_CODE_DESC);

        commResponse.setPagingResult(new PagingResult(
                (int) page.getCurrent(),
                (int) page.getSize(),
                (int) page.getTotal()));
        if (object instanceof List) {
            commResponse.setData(BeanUtil.copyToList((List<?>) object, voClass));
        } else {
            commResponse.setData(BeanUtil.copyObject(object, voClass));
        }
        return commResponse;
    }
}
